﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using SWEN_HMS.Classes;

namespace SWEN_HMS.StaffHousekeepingModule
{
    public partial class ViewSchedule : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["staffid"] == null)
            {
                Response.Redirect("~/UserModule/Login.aspx");
            }
            else if ((Convert.ToString(Session["accessLvl"])) == "No Access")
            {
                Response.Redirect("~/UserModule/Login.aspx");
            }
            else
            {
                if (!Page.IsPostBack)
                {
                    if (Request.QueryString["updateSuccessful"] == "no")
                    {
                        outcomeLabel.Text = "Housekeeping/Schedule ID does not exist!";
                    }
                    ViewByDropDownSelected();
                }
            }
            Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
            Response.Cache.SetValidUntilExpires(false);
            Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetNoStore();
        }

        protected void viewByDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {
            ViewByDropDownSelected();
        }

        protected void ViewByDropDownSelected()
        {
            if (viewByDropDown.SelectedValue == "Day")
            {
                viewByDayDropDown.Visible = true;
                viewByDayDropDown.Enabled = true;
                viewByStaffDropDown.Visible = false;
                viewByStaffDropDown.Enabled = false;
                viewByRoomDropDown.Visible = false;
                viewByRoomDropDown.Enabled = false;
                viewByHousekeepingRoleDropDown.Visible = false;
                viewByHousekeepingRoleDropDown.Enabled = false;
                viewByHousekeepingRoleStaffIDDropDown.Visible = false;
                viewByHousekeepingRoleStaffIDDropDown.Enabled = false;

                viewStaffScheduleMultiView.ActiveViewIndex = 0;

                SelectedDayFunction();
            }
            else if (viewByDropDown.SelectedValue == "Staff")
            {
                viewByDayDropDown.Visible = false;
                viewByDayDropDown.Enabled = false;
                viewByStaffDropDown.Visible = true;
                viewByStaffDropDown.Enabled = true;
                viewByRoomDropDown.Visible = false;
                viewByRoomDropDown.Enabled = false;
                viewByHousekeepingRoleDropDown.Visible = false;
                viewByHousekeepingRoleDropDown.Enabled = false;
                viewByHousekeepingRoleStaffIDDropDown.Visible = false;
                viewByHousekeepingRoleStaffIDDropDown.Enabled = false;

                viewStaffScheduleMultiView.ActiveViewIndex = 1;

                List<int> resultSet = StaffHousekeepingDBManager.getStaffIDHousekeepingList();
                if (resultSet != null)
                {
                    viewByStaffDropDown.DataSource = resultSet;
                    viewByStaffDropDown.DataBind();
                    SelectedStaffFunction();
                }
                else
                {
                    selectedStaffIDLabel.Text = "No schedule found!";
                    selectedStaffNameLabel.Text = "";
                }
            }
            else if (viewByDropDown.SelectedValue == "Room")
            {
                viewByDayDropDown.Visible = false;
                viewByDayDropDown.Enabled = false;
                viewByStaffDropDown.Visible = false;
                viewByStaffDropDown.Enabled = false;
                viewByRoomDropDown.Visible = true;
                viewByRoomDropDown.Enabled = true;
                viewByHousekeepingRoleDropDown.Visible = false;
                viewByHousekeepingRoleDropDown.Enabled = false;
                viewByHousekeepingRoleStaffIDDropDown.Visible = false;
                viewByHousekeepingRoleStaffIDDropDown.Enabled = false;

                viewStaffScheduleMultiView.ActiveViewIndex = 2;

                List<int> resultSet = StaffHousekeepingDBManager.getRoomNumList();
                if (resultSet != null)
                {
                    viewByRoomDropDown.DataSource = resultSet;
                    viewByRoomDropDown.DataBind();
                    SelectedRoomFunction();
                }
                else
                {
                    selectedRoomLabel.Text = "No schedule found!";
                }
            }
            else if (viewByDropDown.SelectedValue == "Housekeeping Role")
            {
                viewByDayDropDown.Visible = false;
                viewByDayDropDown.Enabled = false;
                viewByStaffDropDown.Visible = false;
                viewByStaffDropDown.Enabled = false;
                viewByRoomDropDown.Visible = false;
                viewByRoomDropDown.Enabled = false;
                viewByHousekeepingRoleDropDown.Visible = true;
                viewByHousekeepingRoleDropDown.Enabled = true;
                viewByHousekeepingRoleStaffIDDropDown.Visible = true;
                viewByHousekeepingRoleStaffIDDropDown.Enabled = true;

                SelectedHousekeepingRoleFunction();

                viewStaffScheduleMultiView.ActiveViewIndex = 3;
            }
            else
            {
                viewByDayDropDown.Visible = false;
                viewByDayDropDown.Enabled = false;
                viewByStaffDropDown.Visible = false;
                viewByStaffDropDown.Enabled = false;
                viewByRoomDropDown.Visible = false;
                viewByRoomDropDown.Enabled = false;
                viewByHousekeepingRoleDropDown.Visible = false;
                viewByHousekeepingRoleDropDown.Enabled = false;
                viewByHousekeepingRoleStaffIDDropDown.Visible = false;
                viewByHousekeepingRoleStaffIDDropDown.Enabled = false;
            }
        }

        protected void viewByDayDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedDayFunction();
        }

        protected void SelectedDayFunction()
        {
            selectedDayLabel.Text = viewByDayDropDown.SelectedValue;
            List<Housekeeping> resultSet = StaffHousekeepingDBManager.getScheduleByDay(viewByDayDropDown.SelectedValue);
            if (resultSet != null)
            {
                viewScheduleByDayGridView.DataSource = resultSet;
                viewScheduleByDayGridView.DataBind();
            }
            else
            {
                selectedDayLabel.Text = "No schedule found!";
                viewScheduleByDayGridView.DataSource = resultSet;
                viewScheduleByDayGridView.DataBind();
            }
        }

        protected void SelectedStaffFunction()
        {
            selectedStaffIDLabel.Text = viewByStaffDropDown.SelectedValue;
            selectedStaffNameLabel.Text = StaffHousekeepingDBManager.getStaffNameByStaffIDHousekeeping(Convert.ToInt32(viewByStaffDropDown.SelectedValue));
            stfPositionForViewByStaffLabel.Text = StaffHousekeepingDBManager.getStaffPositionHousekeeping(Convert.ToInt32(viewByStaffDropDown.SelectedValue));
            List<Housekeeping> resultSet = StaffHousekeepingDBManager.getScheduleByStaff(Convert.ToInt32(viewByStaffDropDown.SelectedValue));
            if (resultSet != null)
            {
                viewScheduleByStaffGridView.DataSource = resultSet;
                viewScheduleByStaffGridView.DataBind();
            }
            else
            {
                selectedStaffIDLabel.Text = "No schedule found!";
                selectedStaffNameLabel.Text = "";
                viewScheduleByStaffGridView.DataSource = resultSet;
                viewScheduleByStaffGridView.DataBind();
            }
        }

        protected void SelectedRoomFunction()
        {
            selectedRoomLabel.Text = viewByRoomDropDown.SelectedValue;
            List<Housekeeping> resultSet = StaffHousekeepingDBManager.getScheduleByRoomNo(Convert.ToInt32(viewByRoomDropDown.SelectedValue));
            if (resultSet != null)
            {
                viewScheduleByRoomGridView.DataSource = resultSet;
                viewScheduleByRoomGridView.DataBind();
            }
            else
            {
                selectedRoomLabel.Text = "No schedule found!";
                viewScheduleByRoomGridView.DataSource = resultSet;
                viewScheduleByRoomGridView.DataBind();
            }

        }

        protected void viewByStaffDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedStaffFunction();
        }

        protected void viewByRoomDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedRoomFunction();
        }

        protected void DeleteSchedule(object sender, GridViewDeleteEventArgs ex) // delete by day
        {
            int toDeleteID = Convert.ToInt32(viewScheduleByDayGridView.DataKeys[ex.RowIndex].Value.ToString());
            bool ifDeleteSuccess = StaffHousekeepingDBManager.deleteSchedule(toDeleteID);
            if (ifDeleteSuccess == true)
            {
                outcomeLabel.Text = "Schedule/Housekeeping ID <b><u>" + Convert.ToString(toDeleteID) + "</u></b> has been deleted!";
                SelectedDayFunction();
            }
            else
            {
                outcomeLabel.Text = "Schedule has not been deleted!";
            }
        }

        protected void DeleteSchedule1(object sender, GridViewDeleteEventArgs ex) // delete by staff
        {
            int toDeleteID = Convert.ToInt32(viewScheduleByStaffGridView.DataKeys[ex.RowIndex].Value.ToString());
            bool ifDeleteSuccess = StaffHousekeepingDBManager.deleteSchedule(toDeleteID);
            if (ifDeleteSuccess == true)
            {
                outcomeLabel.Text = "Schedule/Housekeeping ID <b><u>" + Convert.ToString(toDeleteID) + "</u></b> has been deleted!";
                SelectedStaffFunction();
            }
            else
            {
                outcomeLabel.Text = "Schedule has not been deleted!";
            }
        }

        protected void DeleteSchedule2(object sender, GridViewDeleteEventArgs ex) // delete by room
        {
            int toDeleteID = Convert.ToInt32(viewScheduleByRoomGridView.DataKeys[ex.RowIndex].Value.ToString());
            bool ifDeleteSuccess = StaffHousekeepingDBManager.deleteSchedule(toDeleteID);
            if (ifDeleteSuccess == true)
            {
                outcomeLabel.Text = "Schedule/Housekeeping ID <b><u>" + Convert.ToString(toDeleteID) + "</u></b> has been deleted!";
                SelectedRoomFunction();
            }
            else
            {
                outcomeLabel.Text = "Schedule has not been deleted!";
            }
        }

        protected void DeleteSchedule3(object sender, GridViewDeleteEventArgs ex) // delete by housekeeping role
        {
            int toDeleteID = Convert.ToInt32(viewScheduleByHousekeepingRoleGridView.DataKeys[ex.RowIndex].Value.ToString());
            bool ifDeleteSuccess = StaffHousekeepingDBManager.deleteSchedule(toDeleteID);
            if (ifDeleteSuccess == true)
            {
                outcomeLabel.Text = "Schedule/Housekeeping ID <b><u>" + Convert.ToString(toDeleteID) + "</u></b> has been deleted!";
                SelectedHousekeepingRoleByStaffIDFunction();
            }
            else
            {
                outcomeLabel.Text = "Schedule has not been deleted!";
            }
        }

        protected void addScheduleButton_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/StaffHousekeepingModule/AddSchedule.aspx");
        }

        protected void SelectedHousekeepingRoleFunction()
        {
            selectedHousekeepingRoleLabel.Text = viewByHousekeepingRoleDropDown.SelectedValue;
            List<int> resultSet = StaffHousekeepingDBManager.getStaffIDsByHousekeepingRole(viewByHousekeepingRoleDropDown.SelectedValue);
            if (resultSet != null)
            {
                viewByHousekeepingRoleStaffIDDropDown.DataSource = resultSet;
                viewByHousekeepingRoleStaffIDDropDown.DataBind();
                SelectedHousekeepingRoleByStaffIDFunction();
            }
            else
            {
                selectedStaffNameForViewByHousekeepingRolesLabel.Text = "No staff found!";
                viewByHousekeepingRoleStaffIDDropDown.DataSource = resultSet;
                viewByHousekeepingRoleStaffIDDropDown.DataBind();
            }
        }

        protected void SelectedHousekeepingRoleByStaffIDFunction()
        {
            selectedHousekeepingStaffIDLabel.Text = viewByHousekeepingRoleStaffIDDropDown.SelectedValue;
            selectedStaffNameForViewByHousekeepingRolesLabel.Text = StaffHousekeepingDBManager.getStaffNameByStaffIDHousekeeping(Convert.ToInt32(viewByHousekeepingRoleStaffIDDropDown.SelectedValue));
            List<Housekeeping> resultSet = StaffHousekeepingDBManager.getScheduleByStaff(Convert.ToInt32(viewByHousekeepingRoleStaffIDDropDown.SelectedValue));
            if (resultSet != null)
            {
                viewScheduleByHousekeepingRoleGridView.DataSource = resultSet;
                viewScheduleByHousekeepingRoleGridView.DataBind();
            }
            else
            {
                selectedStaffNameForViewByHousekeepingRolesLabel.Text = "No schedule found!";
                selectedHousekeepingStaffIDLabel.Text = "";
                viewScheduleByHousekeepingRoleGridView.DataSource = resultSet;
                viewScheduleByHousekeepingRoleGridView.DataBind();
            }
        }

        protected void viewByHousekeepingRoleDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedHousekeepingRoleFunction();
        }

        protected void viewByHousekeepingRoleStaffIDDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedHousekeepingRoleByStaffIDFunction();
        }
    }
}